package com.huixue.cn.ztej.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.util.StringUtil;
import com.huixue.cn.ztej.persistence.Domain;
import com.huixue.cn.ztej.service.TClassService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.TClassInfo;

@Domain(TClassInfo.class)
@Controller
@RequestMapping(value = "/sys/tclass")
public class TClassController {

	@Autowired(required = true)
	private TClassService tClassService;
	
	@RequestMapping(value = "/list")
	public String list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			TClassInfo searchForm,Model model,HttpServletRequest request) {
		Map<String,Object> searchParams = new HashMap<String,Object>();
		if(StringUtil.isNotEmpty(searchForm.getName())){
			searchParams.put("name", searchForm.getName());
		}
		if(searchForm.getSchoolId()!=null){
			searchParams.put("schoolId", searchForm.getSchoolId());
		}
		if(searchForm.getGradeId()!=null){
			searchParams.put("gradeId", searchForm.getGradeId());
		}
		Results results = tClassService.searchByPage(searchParams,page,rows);
		model.addAttribute("res", results);
		model.addAttribute("searchForm", searchForm);
		return "/sys/class/class_list";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/sys/class/class_add";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate/{id}")
	public String toUpate(@PathVariable ("id")Integer id,Model model,HttpServletRequest request) {
		TClassInfo info = tClassService.getInfo(id);
		model.addAttribute("info", info);
		return "/sys/class/class_edit";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results update(@RequestBody TClassInfo info,Model model,HttpServletRequest request) {
		tClassService.update(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "修改成功！");
		return results;
	}

	@ResponseBody
	@RequestMapping(value = "/add",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results add(@RequestBody TClassInfo info,HttpServletRequest request) {
		info.setCreateTime(new Date());
		 tClassService.save(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "listall", produces = "application/json;charset=UTF-8")
	public String listAll(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> resulMmap = new HashMap<String, Object>();
		List<TClassInfo> list = tClassService.findAll();
		resulMmap.put("list", list);
		resulMmap.put("result", "success");
		return JSON.toJSONString(resulMmap);
	}
	
	@RequestMapping(value = "findclassByGradeId", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String findclassByGradeId(HttpServletRequest request,HttpServletResponse response) {
		String gradeId = String.valueOf(request.getParameter("gradeId"));
		Map<String, Object> resulMmap = new HashMap<String, Object>();
		List<TClassInfo> list = tClassService.findByGradeId(Integer.parseInt(gradeId));
		resulMmap.put("list", list);
		resulMmap.put("result", "success");
		return JSON.toJSONString(resulMmap);
	}
}
